import { createStore} from 'vuex'
import {
  reqLogin,
  reqRegister,
  reqLogout,
  reqCreateCompetition,
  reqUpdateCompetition,
  reqCompetitionList,
  reqSearchCompetition,
  reqDeleteCompetition,
  reqEnrollCompetition,
  reqPlayerInfoUpdate,
  reqAllJudges,
  reqAlgorithm,
  reqAlgorithmRes,
  reqCompetitionPlayerList,
  reqCompetitionPlayerParticipantNum,
  reqCancelEnroll,
  reqAllPlayerInfo,
  reqPLayerEnrollComp,
  reqUpdateCompetitionReferee,
  reqAllPlayerScore,
  reqAllPlayerScoreByCompetition,
  reqGetOwnScores,
  reqGetFileList,
  reqSearchFile,
  reqGetJudgeList,
  reqDeleteJudge,
  reqAddJudge,
  reqAllEnrollPlayerList,
  reqdownloadFile,
  reqUpdatePwd,
  reqVerifyOldPassword,
  reqAllUser,
  reqResetPwd,
  reqDelFile,
  reqSearchUser,
  reqGetUserInfo,
  reqUpdateUserInfo,
  reqUpdateScore,
  reqGetNoticeList,
  reqSetNotice,
  reqCurrentUserInfo,
  reqUpdateMenuRole,
  reqAllMenu,
  reqAllRole,
  reqRoleMenu,
  reqMyMenu,
  reqAddRole,
} from "../api/index";

export default createStore({
  state: {
    tagsList: [],
    collapse: false,

    //身份标识符【存储在vuex】
    token: localStorage.getItem("token"),
    //用户名
    username: localStorage.getItem("username"),
    //身份
    identity: localStorage.getItem("identity"),
  },

  mutations: {
    //标签列表
    delTagsItem(state, data) {
      state.tagsList.splice(data.index, 1);
    },
    setTagsItem(state, data) {
      state.tagsList.push(data);
    },
    clearTags(state) {
      state.tagsList = [];
    },
    closeTagsOther(state, data) {
      state.tagsList = data;
    },
    closeCurrentTag(state, data) {
      for (let i = 0, len = state.tagsList.length; i < len; i++) {
        const item = state.tagsList[i];
        if (item.path === data.$route.fullPath) {
          if (i < len - 1) {
            data.$router.push(state.tagsList[i + 1].path);
          } else if (i > 0) {
            data.$router.push(state.tagsList[i - 1].path);
          } else {
            data.$router.push("/");
          }
          state.tagsList.splice(i, 1);
          break;
        }
      }
    },
    // 侧边栏折叠
    handleCollapse(state, data) {
      state.collapse = data;
    },

    //用户操作
    //设置token
    set_token(state, token) {
      state.token = token;
      localStorage.setItem("token", token);
    },
    //设置用户名
    set_username(state, username) {
      state.username = username;
      localStorage.setItem("username", username);
    },
    set_identity(state, identity) {
      state.identity = identity;
      localStorage.setItem("identity", identity);
    },
    //清除仓库相关用户信息
    clear(state) {
      state.token = "";
      state.username = "";
      state.identity = "";
      //本地存储令牌清空
      localStorage.removeItem("token");
      localStorage.removeItem("username");
      localStorage.removeItem("identity");
    },
  },
  actions: {
    //登录
    async Login({ commit, state, dispatch }, data) {
      let result = await reqLogin(data);
      console.log("登录响应结果", result);
      return result;
    },

    //查询当前用户信息
    async CurrentUserInfo({ commit, state, dispatch }) {
      let result = await reqCurrentUserInfo();
      console.log("查询当前用户信息", result);
      return result;
    },

    //注册
    async Register({ commit, state, dispatch }, data) {
      let result = await reqRegister(data);
      console.log("注册响应结果", result);
      return result;
    },
    //退出登录
    async Logout({ commit, state, dispatch }) {
      let result = await reqLogout();
      return result;
    },
    //创建一项比赛
    async AddCompetition({ commit, state, dispatch }, data) {
      let result = await reqCreateCompetition(data);
      console.log("创建一系列比赛响应结果", result);
      return result;
    },

    //更新一项比赛
    async UpdateCompetition({ commit, state, dispatch }, data) {
      let result = await reqUpdateCompetition(data);
      console.log("更新一项比赛响应结果", result);
      return result;
    },

    //获取比赛列表(带分页)
    async CompetitionList({ commit, state, dispatch }, data) {
      let result = await reqCompetitionList(data);
      console.log("获取比赛列表响应结果", result);
      return result;
    },

    //通过名字模糊查询比赛
    async SearchCompetition({ commit, state, dispatch }, data) {
      let result = await reqSearchCompetition(data);
      console.log("通过名字模糊查询比赛响应结果", result);
      return result;
    },

    //通过名字删除比赛
    async DeleteCompetition({ commit, state, dispatch }, data) {
      let result = await reqDeleteCompetition(data);
      console.log("删除比赛响应结果", result);
      if (result.code === 200) {
        return "ok";
      } else {
        return result.message;
      }
    },

    //报名某项比赛
    async EnrollCompetition({ commit, state, dispatch }, data) {
      let result = await reqEnrollCompetition(data);
      console.log("报名某项比赛响应结果", result);
      if (result.code === 200) {
        return "ok";
      } else {
        return result.message;
      }
    },

    //参赛者注册个人信息
    async PlayerInfoUpdate({ commit, state, dispatch }, data) {
      let result = await reqPlayerInfoUpdate(data);
      console.log("参赛者注册个人信息响应结果", result);
      if (result.code === 200) {
        return "ok";
      } else {
        return result.message;
      }
    },

    //查询所有裁判
    async RefereeList({ commit, state, dispatch }) {
      let result = await reqAllJudges();
      console.log("查询所有裁判响应结果", result);
      return result;
    },

    //编排比赛
    async ArrangeCompetition({ commit, state, dispatch }, data) {
      let result = await reqAlgorithm(data);
      console.log("编排比赛响应结果", result);
      if (result.code === 200) {
        return "ok";
      } else {
        return result.message;
      }
    },

    //获取编排结果
    async GetArrangeResult({ commit, state, dispatch }) {
      let result = await reqAlgorithmRes();
      console.log("获取编排结果响应结果", result);
      if (result.code === 200) {
        return result.obj;
      } else {
        return Promise.reject(new Error(result.message));
      }
    },

    //获取所有比赛参赛者
    async CompetitionPlayerList({ commit, state, dispatch }) {
      let result = await reqCompetitionPlayerList();
      console.log("获取所有比赛参赛者响应结果", result);
      return result;
    },

    //分页获取所有比赛参赛者
    async CompetitionPlayerListByPage({ commit, state, dispatch }, data) {
      let result = await reqAllEnrollPlayerList(data);
      console.log("分页获取所有比赛参赛者响应结果", result);
      return result;
    },

    //获取参赛者参加比赛的数量
    async CompetitionPlayerParticipantNum({ commit, state, dispatch }, data) {
      let result = await reqCompetitionPlayerParticipantNum(data);
      console.log("获取参赛者参加比赛的数量响应结果", result);
      return result;
    },

    //取消报名
    async CancelEnroll({ commit, state, dispatch }, data) {
      let result = await reqCancelEnroll(data);
      console.log("取消报名响应结果", result);
      return result;
    },

    //获取所有选手信息
    async AllPlayerInfo({ commit, state, dispatch }) {
      let result = await reqAllPlayerInfo();
      console.log("获取所有选手信息响应结果", result);
      return result;
    },

    //根据选手编号获取报名项目
    async PLayerEnrollComp({ commit, state, dispatch }, data) {
      let result = await reqPLayerEnrollComp(data);
      console.log("根据选手编号获取报名项目响应结果", result);
      return result;
    },

    //修改裁判
    async UpdateReferee({ commit, state, dispatch }, data) {
      let result = await reqUpdateCompetitionReferee(data);
      console.log("修改裁判响应结果", result);
      return result;
    },

    //获取所有成绩
    async AllPlayerScore({ commit, state, dispatch }, data) {
      let result = await reqAllPlayerScore(data);
      console.log("获取所有成绩响应结果", result);
      return result;
    },

    //获取成绩通过比赛
    async AllPlayerScoreByCompetition({ commit, state, dispatch }, data) {
      let result = await reqAllPlayerScoreByCompetition(data);
      console.log("通过比赛获取成绩响应结果", result);
      return result;
    },

    //更新成绩
    async UpdatePlayerScore({ commit, state, dispatch }, data) {
      let result = await reqUpdateScore(data);
      console.log("更新成绩响应结果", result);
      return result;
    },

    //通过选手id查询比赛成绩
    async GetOwnScores({ commit, state, dispatch }, data) {
      let result = await reqGetOwnScores(data);
      console.log("通过选手id查询比赛成绩响应结果", result);
      return result;
    },

    //获取所有文件
    async GetFileList({ commit, state, dispatch }, data) {
      let result = await reqGetFileList(data);
      console.log("获取所有文件响应结果", result);
      return result;
    },

    //搜索文件
    async SearchFile({ commit, state, dispatch }, data) {
      let result = await reqSearchFile(data);
      console.log("搜索文件响应结果", result);
      return result;
    },

    //下载文件
    async DownloadFile({ commit, state, dispatch }, data) {
      let result = await reqdownloadFile(data);
      console.log("下载文件响应结果", result);
      return result;
    },

    //分页获取裁判列表
    async GetJudgeList({ commit, state, dispatch }, data) {
      let result = await reqGetJudgeList(data);
      console.log("分页获取裁判列表响应结果", result);
      return result;
    },

    //删除裁判
    async DeleteJudge({ commit, state, dispatch }, data) {
      let result = await reqDeleteJudge(data);
      console.log("删除裁判响应结果", result);
      return result;
    },

    //添加裁判
    async AddJudge({ commit, state, dispatch }, data) {
      let result = await reqAddJudge(data);
      console.log("添加裁判响应结果", result);
      return result;
    },

    //修改密码
    async UpdatePassword({ commit, state, dispatch }, data) {
      let result = await reqUpdatePwd(data);
      console.log("修改密码响应结果", result);
      return result;
    },

    //验证密码
    async VerifyPassword({ commit, state, dispatch }, data) {
      let result = await reqVerifyOldPassword(data);
      console.log("验证密码响应结果", result);
      return result;
    },

    //获取所有用户
    async GetAllUser({ commit, state, dispatch }, data) {
      let result = await reqAllUser(data);
      console.log("获取所有用户响应结果", result);
      return result;
    },

    //管理员重置密码
    async ResetPassword({ commit, state, dispatch }, data) {
      let result = await reqResetPwd(data);
      return result;
    },

    //删除文件
    async DeleteFile({ commit, state, dispatch }, data) {
      let result = await reqDelFile(data);
      return result;
    },

    //查询用户
    async SearchUser({ commit, state, dispatch }, data) {
      let result = await reqSearchUser(data);
      console.log("查询用户响应结果", result);
      return result;
    },

    //查询用户个人信息
    async GetUserInfo({ commit, state, dispatch }, data) {
      let result = await reqGetUserInfo(data);
      console.log("查询用户个人信息响应结果", result);
      return result;
    },

    //更新选手个人信息（只有学校）
    async UpdateUserInfo({ commit, state, dispatch }, data) {
      let result = await reqUpdateUserInfo(data);
      console.log("更新选手个人信息响应结果", result);
      return result;
    },

    //分页获取公告信息
    async GetNoticeList({ commit, state, dispatch }, data) {
      let result = await reqGetNoticeList(data);
      console.log("分页查询公告列表结果", result);
      return result;
    },

    //管理员设置公告
    async SetNotice({ commit, state, dispatch }, data) {
      let result = await reqSetNotice(data);
      console.log("管理员设置公告结果", result);
      return result;
    },

    //更新菜单角色管理
    async UpdateMenuRole({ commit, state, dispatch }, data) {
      const result = await reqUpdateMenuRole(data);
      console.log("更新菜单角色管理结果", result);
      return result;
    },

    //获取菜单
    async GetMenu({ commit, state, dispatch }) {
      let result = await reqAllMenu();
      console.log("获取菜单响应结果", result);
      return result;
    },

    //获取所有角色
    async GetAllRole({ commit, state, dispatch }) {
      let result = await reqAllRole();
      return result;
    },

    //获取某一角色菜单
    async GetRoleMenu({ commit, state, dispatch }, data) {
      let result = await reqRoleMenu(data);
      return result;
    },

    //根据用户id获取菜单
    async GetMyMenu({ commit, state, dispatch }) {
      let result = await reqMyMenu();
      console.log("根据用户id获取菜单响应结果",result);
      return result;
    },

    //添加角色
    async AddRole({ commit, state, dispatch }, data) {
      let result = await reqAddRole(data);
      return result;
    },
  },

  modules: {},
});